package org.jeecg.modules.qy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.qy.entity.QyAccount;
import org.jeecg.modules.qy.mapper.QyAccountMapper;
import org.jeecg.modules.qy.service.IQyAccountService;
import org.springframework.stereotype.Service;

/**
 * @Description: 公司权益
 * @Author: jeecg-boot
 * @Date:   2021-03-09
 * @Version: V1.0
 */
@Service
public class QyAccountServiceImpl extends ServiceImpl<QyAccountMapper, QyAccount> implements IQyAccountService {


    @Override
    public IPage<QyAccount> selectQyAccountPage(QyAccount qyAccount,int pageNo, int pageSize){
        QueryWrapper<QyAccount> em = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(qyAccount.getCompanyName())&&!"**".equals(qyAccount.getCompanyName())){
            em.like("qc.company_name",qyAccount.getCompanyName().replaceAll("\\*",""));
        }
        return baseMapper.selectQyAccountList(new Page<>(pageNo,pageSize),em);
    }

    @Override
    public void updateAccount(QyAccount qyAccount){
        if(qyAccount.getAddCommoditySurplusNum()!=null && qyAccount.getAddCommoditySurplusNum()>0){
            if(qyAccount.getAddType()==1){
                qyAccount.setCommoditySurplusNum(qyAccount.getCommoditySurplusNum()+qyAccount.getAddCommoditySurplusNum());
            }else{
                qyAccount.setMuchCommoditySurplusNum(qyAccount.getMuchCommoditySurplusNum()+qyAccount.getAddCommoditySurplusNum());
            }
            updateById(qyAccount);
        }

    }

    public QyAccount getByCompanyId(String companyId){
        QueryWrapper<QyAccount> em = new QueryWrapper<>();
        em.eq("company_id",companyId);
        return getOne(em);
    }
}
